package com.ymatou.iapi.deviceid.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.deviceid.parameter.GetBean;
import com.ymatou.iapi.deviceid.parameter.SaveBean;
import com.ymatou.iapi.deviceid.service.GetCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DeviceidCallService;
import com.ymttest.database.mongo.DeviceidBean;
import com.ymttest.database.mongo.DeviceidWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 根据deviceid获取deviceinfo接口用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Get {
	private static GetBean getBean;
	private static GetCall getCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据deviceid获取deviceinfo接口用例");
	}

	@Before
	public void caseUp() {
		getBean = new GetBean();
		getCall = new GetCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Get_001() {
		Logger.start(true, "根据deviceid获取deviceinfo");
		try {

			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean);

			getBean.setDeviceId(saveBean.getDeviceid());

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(saveBean.getDeviceid() + " exists", getCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");
				
				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Logger.verifyEquals(newDate, getCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Get_002() {
		Logger.start(true, "根据deviceid获取deviceinfo");
		try {

			SaveBean saveBean = DeviceidCallService.setDefaultSaveBean();
			DeviceidCallService.saveCall(saveBean);

			getBean.setDeviceId(saveBean.getDeviceid());

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(saveBean.getDeviceid() + " exists", getCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");

				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Logger.verifyEquals(newDate, getCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Get_003() {
		Logger.start(false, "根据deviceid获取deviceinfo-deviceid不存在");
		try {
			getBean.setDeviceId("11111");

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("102", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("11111  not exists", getCall.getMSG(), "验证返回msg");
			Logger.verifyEquals("null", getCall.getData(), "验证返回data");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Get_004() {
		Logger.start(false, "根据deviceid获取deviceinfo-deviceid不传");
		try {
			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("100", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("参数不能为空", getCall.getMSG(), "验证返回msg");
			Logger.verifyEquals("null", getCall.getData(), "验证返回data");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Get_005() {
		Logger.start(true, "多个相同的deviceId, GET接口返回activeTime较小的一个,type不传默认0");
		try {

			SaveBean saveBean1 = DeviceidCallService.setDefaultFullSaveBean();
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setDeviceid(saveBean1.getDeviceid());
			DeviceidCallService.saveCall(saveBean1);
			Thread.sleep(1000);
			DeviceidCallService.saveCall(saveBean);

			getBean.setDeviceId(saveBean.getDeviceid());

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(saveBean.getDeviceid() + " exists", getCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(2, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");
				
				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Logger.verifyEquals(newDate, getCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Get_006() {
		Logger.start(true, "多个相同的deviceId,type=0,接口返回activeTime最小的一个");
		try {

			SaveBean saveBean1 = DeviceidCallService.setDefaultFullSaveBean();
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setDeviceid(saveBean1.getDeviceid());
			DeviceidCallService.saveCall(saveBean1);
			Thread.sleep(1000);
			DeviceidCallService.saveCall(saveBean);

			getBean.setDeviceId(saveBean.getDeviceid());

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(saveBean.getDeviceid() + " exists", getCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(2, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");
				
				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Logger.verifyEquals(newDate, getCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Get_007() {
		Logger.start(true, "多个相同的deviceId,type=1,接口返回activeTime最大的一个");
		try {

			SaveBean saveBean1 = DeviceidCallService.setDefaultFullSaveBean();
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setDeviceid(saveBean1.getDeviceid());
			DeviceidCallService.saveCall(saveBean1);
			Thread.sleep(1000);
			DeviceidCallService.saveCall(saveBean);

			getBean.setDeviceId(saveBean.getDeviceid());

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(saveBean.getDeviceid() + " exists", getCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(2, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(1);
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");
				
				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Logger.verifyEquals(newDate, getCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Get_008() {
		Logger.start(true, "多个相同的deviceId,type=1,接口返回activeTime最大的一个");
		try {

			getBean.setDeviceId("510DEE11-8BCE-4C50-B012-D1BAB8AED30A");

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
//			Logger.verifyEquals(saveBean.getDeviceid() + " exists", getCall.getMSG(), "验证返回Message");
//
//			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());
//
//			Logger.verifyEquals(2, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");
//
//			if (deviceidBeans.size() == 1) {
//				DeviceidBean bean = deviceidBeans.get(1);
//				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
//				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");
//				
//				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
//				int hour=Integer.parseInt(date.substring(11, 13))+8;
//	
//				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
//				
//				Logger.verifyEquals(newDate, getCall.getActiveTime(),
//						"验证返回activeTime");
//				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
//				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
//						"验证返回signVerified");
//				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
//				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
//			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Get_009() {
		Logger.start(true, "根据deviceid获取deviceinfo,不区分大小写");
		try {

			SaveBean saveBean = DeviceidCallService.setDefaultSaveBean();
			saveBean.setDeviceid(TestDataManager.getRandomUUID().toString());
			DeviceidCallService.saveCall(saveBean);

			getBean.setDeviceId(saveBean.getDeviceid().toUpperCase());

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");

				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Logger.verifyEquals(newDate, getCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
			}
			
			
			getBean.setDeviceId(saveBean.getDeviceid().toLowerCase());

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(saveBean.getDeviceid() + " exists", getCall.getMSG(), "验证返回Message");

			deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");

				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Logger.verifyEquals(newDate, getCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Get_010() {
		Logger.start(true, "JIRACONN-17427根据deviceid获取deviceinfo增加machinename字段");
		try {

			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setMachinename("iphoneX1");
			DeviceidCallService.saveCall(saveBean);

			getBean.setDeviceId(saveBean.getDeviceid());

			getCall.setData(getBean);
			getCall.callService();

			Logger.verifyEquals("0", getCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(saveBean.getDeviceid() + " exists", getCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
				Logger.verifyEquals("null", getCall.getErrorMessage(), "验证返回errorMessage");
				
				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Logger.verifyEquals(newDate, getCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals(bean.getMachinename(), getCall.getMachinename(), "验证返回Machinename");
				Logger.verifyEquals("true", getCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getCall.getErrorCode(), "验证返回errorCode");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}